import { 
    allPreviewLocalReceipt,
    allSeaCmbReceipt,
    allSeaReceipt,
    allisReceipt,
    allImportSttleUser,
    allImportTaskSttleBill,
    exportSttleGrantExecl,
    allImportNoDeliverables
} from '@/api/manage/sttle'
import {
    taskExprotFailUser
} from '@/api/manage/task'
import qs from 'qs'
 import { 
  previewFile
} from '@/utils/util'
import axios from 'axios/dist/axios.min'
import JSZip from 'jszip'
import FileSaver from 'file-saver'
const getFile = url => {
    return new Promise((resolve, reject) => {
        axios({
            method:'get',
            url,
            responseType: 'arraybuffer'
        }).then(data => {
            resolve(data.data)
        }).catch(error => {
            resolve();
           // reject(error.toString())
        })
    })
}
const park = {
    actions: {
        allPreviewLocalReceipt({ commit },settleBatchCode) {
            return new Promise((resolve, reject) => {
                allPreviewLocalReceipt(settleBatchCode).then(res => {
                    if (res.code === '1') {
                        previewFile(res.data);
                        resolve();
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allSeaCmbReceipt({ commit },settleBatchCode) {
            return new Promise((resolve, reject) => {
                allSeaCmbReceipt(settleBatchCode).then(res => {
                    if (res.code === '1') {
                       /*  previewFile(res.data);
                        resolve(); */
                        let datas = res.data;
                        var fileList = [];
                        datas.forEach((item, i) => {
                            if (item&&item.indexOf('null') == -1) {
                                let poIndex= item.lastIndexOf("/");
                                let fileName = item.substr(poIndex+1);
                                let pieName = (i+1)+'_'+fileName
                                fileList.push({url:item,file_name:pieName,idx:(i+1)})
                            }
                        })
                        fileList.sort(function(a,b){
                            return a['idx'] - b['idx'];
                        })
                        var zip = new JSZip();
                        const cache = {}
                        let promises = [];
                        if (fileList.length > 0) {
                            fileList.forEach(item => {
                                const promise = getFile(item.url).then(data => {
                                    if (data) {
                                        zip.file(item.file_name, data, { binary: true })
                                        cache[item.file_name] = data
                                    }
                                })
                                promises.push(promise)
                            })
                            Promise.all(promises).then(() => {
                                zip.generateAsync({ type: "blob" }).then(content => {
                                    resolve();
                                    FileSaver.saveAs(content, "回执单.zip")
                                })
                            })
                        }
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allSeaReceipt({ commit },settleBatchCode) {
            return new Promise((resolve, reject) => {
                allSeaReceipt(settleBatchCode).then(res => {
                    if (res.code === '1') {
                        previewFile(res.data);
                        resolve();
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allisReceipt({ commit },detailId) {
            return new Promise((resolve, reject) => {
                allisReceipt(detailId).then(res => {
                    if (res.code === '1') {
                        previewFile(res.data);
                        resolve();
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allImportSttleUser({ commit }, params) {
            return new Promise((resolve, reject) => {
                allImportSttleUser(params).then(response => {
                    let link = document.createElement('a');
                    var blob = new Blob([response],{ type: 'application/vnd.ms-excel'});
                    link.download = params.all == 1 ? "结算名单明细.xls" :'结算失败名单.xls';
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    resolve();
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allImportNoDeliverables({ commit }, params) {
            return new Promise((resolve, reject) => {
                allImportNoDeliverables(params).then(response => {
                    let link = document.createElement('a');
                    var blob = new Blob([response],{ type: 'application/vnd.ms-excel'});
                    link.download = "无交付物用户明细.xls";
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    resolve();
                }).catch(error => {
                    reject(error)
                })
            })
        },
        allImportTaskSttleBill({ commit }, selectedRowKeys) {
            let param = { settleBatchCodes: selectedRowKeys}
            var params = qs.stringify(param, { arrayFormat: 'repeat' });
            return new Promise((resolve, reject) => {
                allImportTaskSttleBill(params).then(res => {
                    if (res.code === '1') {
                        resolve();
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        exportSttleGrantExecl({ commit }, params) {
            if(params.time && params.time.length > 0){
                params.beginTime = params.time[0];
                params.endTime = params.time[1];
            }
            return new Promise((resolve, reject) => {
                exportSttleGrantExecl(params).then(response => {
                    let link = document.createElement('a');
                    var blob = new Blob([response],{ type: 'application/vnd.ms-excel'});
                    link.download = "发放明细.xls";
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    resolve(true);
                }).catch(error => {
                    reject(error)
                })
            })
        },
        taskExprotFailUser({ commit }, params) {
            return new Promise((resolve, reject) => {
                taskExprotFailUser(params).then(response => {
                    let link = document.createElement('a');
                    var blob = new Blob([response],{ type: 'application/vnd.ms-excel'});
                    link.download = "失败人员名单.xls";
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    resolve(true);
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}
export default park
